package com._54programer.demo.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author SN
 * @date 2019/11/28
 * @description
 * 使用ByteBuffer和FileChannel将"Hello Nio"写入到E:\a.txt文件中
 */
public class Demo1 {
    public static void main(String[] args) throws Exception{

        //1.创建输出流和通道
        FileOutputStream fos = new FileOutputStream("E:\\a.txt");
        FileChannel channel = fos.getChannel();

        //2.将数据写入ByteBuffer缓冲区
        String str = "Hello Nio";
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();

        //3.将缓冲区的数据写入到通道
        channel.write(byteBuffer);
        fos.close();
    }
}
